package com.itheima.Test;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class Test004客户端 {
    //请编写程序，实现使用SocketChannel和ServerSocketChannel实现网络编程发送数据的例子。
    public static void main(String[] args) throws IOException {
        SocketChannel sc = SocketChannel.open();
        //连接ip和端口
        sc.connect(new InetSocketAddress("192.168.50.113",1315));
        //创建数组
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //给数组添加数据;
        buffer.put("你好".getBytes());
        //切换;
        buffer.flip();
        //写数据;
        sc.write(buffer);
        ByteBuffer buff = ByteBuffer.allocate(1024);
        int read = sc.read(buff);
        System.out.println(new String(buff.array(),0,read));

        //关流
        sc.close();

    }
}
